/**
 * 作者: heweiping
 * 日期: 2025-04-22 18:42:05
 * 备注: 用户万能活动数据
 */
package com.motu.monstercity.server.game.userdata;

import com.motu.monstercity.protocol.Common.PBUserActConvention;
import com.motu.vertx.module.utility.model.BaseModel;
import com.motu.vertx.module.utility.model.Model;
import io.vertx.core.json.JsonObject;
import io.vertx.core.json.JsonArray;

public class UserActConventionModel extends BaseModel implements Model {
    public static final String LISTKEY = "";
    public static final int REDIS_EXPIRE_TIME = 0;// redis过期时间
    public static final int UPDATE_DB_TYPE = UPDATE_DB_TYPE_QUEUE;// 数据入库类型 队列入库
    public static final boolean ORDER_LIST = false;// 是否需要额外的有序列表

    protected long id; // 玩家id
    protected long actId; // 活动ID
    protected long score; // 分数
    protected int groupId; // 分组ID

    public void reset() {
    }

    protected String getKey() {
        return "UserActConvention." + id;
    }

    protected String getListKey() {
        return null;
    }

    protected String getListItemKey() {
        return null;
    }

    public void doCreate() {
        setTableId();
        insert();
    }

    public void setTableId() {
        putId(incrTableId("user_act_convention"));
    }

    public JsonObject insert() {
        return super.insert(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, LISTKEY, getListKey(), getListItemKey(), ORDER_LIST);
    }

    public void delete() {
        super.delete(getKey(), id, UPDATE_DB_TYPE, id, LISTKEY, getListKey(), getListItemKey(), ORDER_LIST);
    }

    /**
     * 根据入库类型更新
     */
    public JsonObject update() {
        return super.update(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, id, getAllObj(), LISTKEY, getListKey(), getListItemKey());
    }

    /**
     * 实时入库
     */
    public JsonObject updateNow() {
        return super.update(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE_NOW, 0, getAllObj(), LISTKEY, getListKey(), getListItemKey());
    }

    /**
     * 实时入队列
     */
    public JsonObject updateQueueNow() {
        return super.update(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, 0, getAllObj(), LISTKEY, getListKey(), getListItemKey());
    }

    public JsonObject getAllObj(){
        JsonObject obj = new JsonObject();
        obj.put("id", id); // 玩家id
        obj.put("actId", actId); // 活动ID
        obj.put("score", score); // 分数
        obj.put("groupId", groupId); // 分组ID
        return obj;
    }

    public PBUserActConvention.Builder takeInitBuilder() {
        PBUserActConvention.Builder sb = PBUserActConvention.newBuilder();
        if (score != 0) {
            sb.setScore(score);
        }
        return sb;
    }

    public PBUserActConvention.Builder takeBuilder() {
        if (updateObj == null || updateObj.isEmpty()) {
            return takeInitBuilder();
        }
        PBUserActConvention.Builder sb = PBUserActConvention.newBuilder();
        if (isFieldUpdate("score")) {
            sb.setScore(score);
        }
        return sb;
    }

    public long getId() {
        return id;
    }

    public void putId(long id) {
        if (this.id != id) {
            this.id = id;
            updateRedisHashField(getKey(), "id", this.id);
        }
    }

    public void addId(long value) {
        this.id += value;
        updateRedisHashField(getKey(), "id", this.id);
    }

    public long getActId() {
        return actId;
    }

    public void putActId(long actId) {
        if (this.actId != actId) {
            this.actId = actId;
            updateRedisHashField(getKey(), "actId", this.actId);
        }
    }

    public void addActId(long value) {
        this.actId += value;
        updateRedisHashField(getKey(), "actId", this.actId);
    }

    public long getScore() {
        return score;
    }

    public void putScore(long score) {
        if (this.score != score) {
            this.score = score;
            updateRedisHashField(getKey(), "score", this.score);
        }
    }

    public void addScore(long value) {
        this.score += value;
        updateRedisHashField(getKey(), "score", this.score);
    }

    public int getGroupId() {
        return groupId;
    }

    public void putGroupId(int groupId) {
        if (this.groupId != groupId) {
            this.groupId = groupId;
            updateRedisHashField(getKey(), "groupId", this.groupId);
        }
    }

    public void addGroupId(int value) {
        this.groupId += value;
        updateRedisHashField(getKey(), "groupId", this.groupId);
    }

}